package com.meringer.beans;

import java.util.ArrayList;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.meringer.beans.ejb.ProfileEJB;
import com.meringer.beans.ejb.RegisterEJB;
import com.meringer.beans.ejb.UserEJB;
import com.meringer.entities.User;
import com.meringer.utils.CarAndModel;

@ManagedBean(name = "profileBean")
@RequestScoped
public class ProfileBean {
	@EJB
	ProfileEJB profileEJB;
	@EJB
	UserEJB userEJB;
	private String name ;
	
	private Integer age;
	
	private String imagePath;
	@PostConstruct
	public void init () {
		this.name = userEJB.currentUser().getRealName();
		this.age = userEJB.currentUser().getAge();
	}
	public String getImage() {
		return imagePath;
	}
	public void setImage(String image) {
		this.imagePath = image;
	}
	public ArrayList<String> getAllAvatars(){
		Set<String> set = FacesContext.getCurrentInstance().getExternalContext().getResourcePaths("/img/avatars");
		ArrayList<String> avatars = new ArrayList<String>();
		for(String entity : set){
			avatars.add(entity.replaceFirst("/", "")); 
		}
		return avatars;
	}
	public void updateImage(String imagePath){
		profileEJB.updateUserImg(imagePath);
	}
	public void saveName(){
		System.out.println("name changed");
	}
	public void saveAccount(){
		System.out.println(name);
		profileEJB.updateUserNameAndAge(name,age);
	}
//	public UserLastRaces getRaceResults(){
//		profileEJB.getRaceResults();
//	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}
